#include "./../inc/file.h"
#include "./../inc/lcd.h"
#include "./../inc/touch.h"

int *getBMP_IMG(char *path, int width, int height)
{
    FILE *fp = fopen(path, "rb");
    if (fp == NULL)
    {
        perror("BMP_fopen");
        return NULL;
    }
    printf("open %s success\n", path);

    fseek(fp, 54, SEEK_SET);

    char buf_bmp[height][width * 3];

    memset(buf_bmp, 0, height * width * 3);

    int padding = (4 - (width * 3) % 4) % 4; // 计算每行填充字节数

    // 逐行读取 bmp图片每一行的字节数必须是4的倍数
    for (int i = 0; i < height; i++)
    {
        fread(buf_bmp[i], width * 3, 1, fp);
        if (padding > 0)
        {
            fseek(fp, padding, SEEK_CUR); // 跳过填充字节
        }
    }
    fclose(fp);

    int *buf = malloc(height * width * sizeof(int));

    // 处理像素数据，BMP是从下到上存储的
    for (int i = 0; i < height; i++)
    {
        int cow = height - i - 1; // 翻转Y坐标
        for (int j = 0; j < width; j++)
        {
            // 注意：BMP存储顺序是BGR
            int b = buf_bmp[i][j * 3];
            int g = buf_bmp[i][j * 3 + 1];
            int r = buf_bmp[i][j * 3 + 2];

            // 组合成32位ARGB格式（Alpha=0）
            buf[cow * width + j] = (0x00 << 24) | (r << 16) | (g << 8) | b;
        }
    }
    return buf;
}

void put_bmp(int *buf, LCDBuffer lcd_mmap, int IMGCount, int currIMG)
{
    int currLength = 600 / (IMGCount + 1);
    for (int row = 0; row < 480; row++)
    {
        for (int col = 0; col < 800; col++)
        {
            if (row > 440 && row < 451 && col > 149 && col < 649)
            {
                if (col > 149 + (currLength * currIMG) && col < 149 + (currLength * (currIMG + 1)))
                {
                    lcd_mmap[row][col] = BLACK;
                }
                else
                {
                    lcd_mmap[row][col] = WHITE;
                }
            }
            else
            {
                lcd_mmap[row][col] = buf[row * 800 + col];
            }
        }
    }
}

void show_bmp(file_node_p headNode, int currIMG, LCDBuffer lcd_mmap, int direction, int touch_type, int IMGCount)
{
    file_node_p currNode = find_file_node(headNode, currIMG);
    char path[128];
    strcpy(path, currNode->path);
    strcat(path, currNode->name);
    int *buf = getBMP_IMG(path, 800, 480);

    if (direction == LEFT)
    {
        if (touch_type == SINGLE_CLICK)
            put_bmp_left(buf, lcd_mmap, IMGCount, currIMG);
        else
            put_bmp_right(buf, lcd_mmap, IMGCount, currIMG);
    }
    else if (direction == RIGHT)
    {
        if (touch_type == SINGLE_CLICK)
            put_bmp_right(buf, lcd_mmap, IMGCount, currIMG);
        else
            put_bmp_left(buf, lcd_mmap, IMGCount, currIMG);
    }
    else if (direction == NONE)
    {
        put_bmp(buf, lcd_mmap, IMGCount, currIMG);
    }
    free(buf);
}

void put_bmp_left(int *buf, LCDBuffer lcd_mmap, int IMGCount, int currIMG)
{
    int currLength = 600 / (IMGCount + 1);
    for (int visible_cols = 100; visible_cols <= 800; visible_cols += 100)
    {
        for (int col = 0; col < visible_cols; col++)
        {
            int src_col = 799 - col;

            for (int row = 0; row < 480; row++)
            {
                int dst_col = col;
                if (row > 440 && row < 451 && dst_col > 149 && dst_col < 649)
                {
                    if (dst_col > 149 + (currLength * currIMG) && dst_col < 149 + (currLength * (currIMG + 1)))
                    {
                        lcd_mmap[row][dst_col] = BLACK;
                    }
                    else
                    {
                        lcd_mmap[row][dst_col] = WHITE;
                    }
                }
                else
                {
                    lcd_mmap[row][dst_col] = buf[row * 800 + src_col];
                }
            }
        }
    }
}

void put_bmp_right(int *buf, LCDBuffer lcd_mmap, int IMGCount, int currIMG)
{
    int currLength = 600 / (IMGCount + 1);
    for (int visible_cols = 100; visible_cols <= 800; visible_cols += 100)
    {
        for (int col = 0; col < visible_cols; col++)
        {
            int src_col = col;

            for (int row = 0; row < 480; row++)
            {
                int dst_col = 799 - col;
                if (row > 440 && row < 451 && dst_col > 149 && dst_col < 649)
                {
                    if (dst_col > 149 + (currLength * currIMG) && dst_col < 149 + (currLength * (currIMG + 1)))
                    {
                        lcd_mmap[row][dst_col] = BLACK;
                    }
                    else
                    {
                        lcd_mmap[row][dst_col] = WHITE;
                    }
                }
                else
                {
                    lcd_mmap[row][dst_col] = buf[row * 800 + src_col];
                }
            }
        }
    }
}
